package com.honglu.backend.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.honglu.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.honglu.backend.bo.UUserBo;
import com.honglu.backend.common.Constants;
import com.honglu.backend.common.controller.BaseController;
import com.honglu.backend.common.shiroToken.TokenManager;
import com.honglu.backend.service.impl.UUserServiceImpl;
import com.honglu.backend.utils.CodeUtils;
import com.honglu.backend.utils.JSONUtils;
import com.honglu.backend.utils.MyDES;
import com.honglu.utils.SpringUtils;


/**
 * 后台用户Controller
 * @author xiajiyun
 *
 */
@Controller
@RequestMapping(value = "/backend/web/uuser")
public class UUserController extends BaseController {
	
	private static Logger logger=Logger.getLogger(UUserController.class);
	
	@Autowired
	private UUserServiceImpl uUserServiceImpl;  // 用戶service

	@Autowired
	private RedisUtils redisUtils;
	
//	@Autowired
//	private  UserRoleServiceImpl userRoleServiceImpl;// 用戶角色 
	
	/**
	 * 查询所有
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/listAll.do", method = RequestMethod.POST)
	public void listAll(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			int count=uUserServiceImpl.listAllCount(uUserBo);
			List<UUserBo> list = uUserServiceImpl.listAll(uUserBo);
			JSONUtils.toListJSON(response, list, count);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
			return ;
		}
	}
	
	/**
	 * 根据id获取对象信息
	 * @param request
	 * @param response
	 * @param uUserBo
	 */
	@RequestMapping(value = "/getObjectById.do", method = RequestMethod.POST)
	public void getObjectById(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			UUserBo userBo= uUserServiceImpl.getObjectById(uUserBo);// 查詢用戶信息表
			
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			LinkedHashMap<String, Object> map1 = new LinkedHashMap<String, Object>();
			map.put(Constants.CODE, CodeUtils.SUCCESS.getCode());
			map.put(Constants.MESSAGE, CodeUtils.SUCCESSNEWS.getMessage());
			map1.put(Constants.SUCCESSED, CodeUtils.SUCCESS.getFlag());
			userBo.setPassword(MyDES.decryptBasedDes(userBo.getPassword()));//解密
			map1.put("userObject",userBo); // 传入对象
			map.put(Constants.DATA, map1);
			SpringUtils.renderJson(response,map);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
			return ;
		}
	}
	
	
	 
	/**
	 * 保存/修改
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public void save(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			if(uUserBo != null && uUserBo.getId()!= null){// 修改
				boolean flag = uUserServiceImpl.update(uUserBo);
				if(flag){
					JSONUtils.toJSON(response, CodeUtils.UPDATE_SUCCESS);
					return;
				}else{
					JSONUtils.toJSON(response, CodeUtils.UPDATE_FAIL);
					return;
				}
			}else{// 新增
				
				//验证登录帐号，不能重复
				int countUserName = uUserServiceImpl.getCountUserName(uUserBo);
				if(countUserName > 0){// 已存在
					JSONUtils.toJSON(response, CodeUtils.NAME_FAIL);
					return;
				}
				
				boolean flag = uUserServiceImpl.save(uUserBo);
				if(flag){
					JSONUtils.toJSON(response, CodeUtils.SAVE_SUCCESS);
					return;
				}else{
					JSONUtils.toJSON(response, CodeUtils.SAVE_FAIL);
					return ;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
		
	}
	
	
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param uUserBo
	 */
	@RequestMapping(value = "delete.do", method = RequestMethod.POST)
	public void delete(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			boolean flag = uUserServiceImpl.delete(uUserBo);
			if(flag){
				JSONUtils.toJSON(response, CodeUtils.DELETE_SUCCESS);
				return ;
			}else{
				JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
				return ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
	}
	
	
	/**
	 * 多选删除
	 * @param request
	 * @param response
	 * @param uUserBo
	 */
	@RequestMapping(value = "deleteAll.do", method = RequestMethod.POST)
	public void deleteAll(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			boolean flag = uUserServiceImpl.deleteAll(uUserBo);
			if(flag){
				JSONUtils.toJSON(response, CodeUtils.DELETE_SUCCESS);
				return ;
			}else{
				JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
				return ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
	}
	
	
	
	/**
	 * 获取当前登录帐号的昵称和头像
	 * @param request
	 * @param response
	 * @param uUserBo
	 */
	@RequestMapping(value = "/getUser.do", method = RequestMethod.POST)
	public void getUser(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			UUserBo user= TokenManager.getUser();// 直接从token取
			
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			LinkedHashMap<String, Object> map1 = new LinkedHashMap<String, Object>();
			map.put(Constants.CODE, CodeUtils.SUCCESS.getCode());
			map.put(Constants.MESSAGE, CodeUtils.SUCCESSNEWS.getMessage());
			map1.put(Constants.SUCCESSED, CodeUtils.SUCCESS.getFlag());
			
			map1.put("user",user); // 传入对象
			map.put(Constants.DATA, map1);
			
//			map.put("user",userBo); // 传入对象
//			map.put(Constants.DATA, map);
			SpringUtils.renderJson(response,map);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
			return ;
		}
	}
	
	
	/**
	 * 文件上传
	 * @param request
	 * @param response
	 * @param uUserBo
	 */
	@RequestMapping(value = "upload.do", method = RequestMethod.POST)
	public void upload(HttpServletRequest request, HttpServletResponse response, UUserBo uUserBo){
		try {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multiRequest.getFile("file");
			
			String fileVal = uploadFiles(multiRequest, response, file);
			if(StringUtils.isNotEmpty(fileVal) && "error".equals(fileVal)){
				JSONUtils.toJSON(response, CodeUtils.IMAGE_UPLOAD_FAILUE_TYPE);
				return ;
			}
			JSONUtils.toJSON(response, CodeUtils.SUCCESS, fileVal);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return;
		}
	}

	/**
	 * 删除 redis key
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteRedisKey.do",produces = {"text/json;charset=utf-8"}, method = {RequestMethod.POST, RequestMethod.GET})
	public void deleteRedisKey(HttpServletRequest request, HttpServletResponse response){
		try {
			Set<String> sets = redisUtils.keysList("user:token*");
			for (String string : sets) {
				redisUtils.del(string);
			}
			JSONUtils.toJSON(response, CodeUtils.DELETE_SUCCESS);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
			return;
		}
	}

}
